

#### Causal Mediation Analysis


library(dplyr)
library(mediation)
library(stargazer)
library(lme4)
library(ggplot2)


load("a1r_data_analysis.RData")
# mediator prediction: follow campaign


follow_mediator= lmer(follow_campaign_all ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                      data = a1r_analysis_total[a1r_analysis_total$time > 2,])
summary(follow_mediator)


# outcome: vote at all 

will_vote2_out = glmer(will_vote~ follow_campaign_all+ populism_score + delegate + ideology_score_t1 +college_grad +male+white + over60 + lowinc+ (1|state_id) , 
                       data = a1r_analysis_total[a1r_analysis_total$time > 2,], family = 'binomial')
summary(will_vote2_out)

med.follow.vote = mediate(follow_mediator, will_vote2_out, treat = "delegate", mediator = "follow_campaign_all",
                          robustSE = T, sims = 1000)
summary(med.follow.vote)
plot(med.follow.vote)


# outcome: vote for biden 
# null finding
follow_mediator_2= lmer(follow_campaign_all ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white+ (1|state_id) , 
                        data = a1r_analysis_total[a1r_analysis_total$time > 2 & 
                                                    is.na(a1r_analysis_total$simple_vote_biden) == F,])
summary(follow_mediator_2)
vote_for_biden_out = glmer(simple_vote_biden ~ follow_campaign_all+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white+ (1|state_id) , 
                           data = a1r_analysis_total[a1r_analysis_total$time > 2,], family = 'binomial')
summary(vote_for_biden_out)

med.follow.vote.b = mediate(follow_mediator_2, vote_for_biden_out, treat = "delegate", mediator = "follow_campaign_all",
                            robustSE = T, sims = 1000)
summary(med.follow.vote.b)
plot(med.follow.vote.b)


stargazer(follow_mediator, follow_mediator_2, will_vote2_out, vote_for_biden_out, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table1.html")


### covid outcome
follow_mediator_3= lmer(follow_campaign_all ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ (1|state_id) , 
                        data = a1r_analysis_total[a1r_analysis_total$time > 2 & 
                                                    is.na(a1r_analysis_total$covid_response_all) == F,])

covid_out = lmer(covid_response_all  ~ follow_campaign_all+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ (1|state_id) , 
                 data = a1r_analysis_total[a1r_analysis_total$time > 2,])
summary(covid_out)

med.follow.covid = mediate(follow_mediator_3, covid_out, treat = "delegate", mediator = "follow_campaign_all",
                           robustSE = T, sims = 1000)
summary(med.follow.covid)
plot(med.follow.covid)









# mediator prediction: worth_listening


worth_mediator= lmer(worth_listening ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white+ (1|state_id) , 
                     data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                 is.na(a1r_analysis_total$will_vote) == F,])
summary(worth_mediator)




will_vote2_out_worth = glmer(will_vote~ worth_listening+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white+ (1|state_id) , 
                             data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                         is.na(a1r_analysis_total$will_vote) == F,], family = 'binomial')
summary(will_vote2_out_worth)

med.worth.vote = mediate(worth_mediator, will_vote2_out_worth, treat = "delegate", mediator = "worth_listening",
                         robustSE = T, sims = 1000)
summary(med.worth.vote)
plot(med.worth.vote)

worth_mediator2= lmer(worth_listening ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                      data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                  is.na(a1r_analysis_total$simple_vote_biden) == F,])
summary(worth_mediator2)

vote_for_biden_ou_worth = glmer(simple_vote_biden ~ worth_listening+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white + (1|state_id) , 
                                data = a1r_analysis_total[a1r_analysis_total$time > 2,], family = 'binomial')
summary(vote_for_biden_ou_worth)

med.worth.vote.b = mediate(worth_mediator2, vote_for_biden_ou_worth, treat = "delegate", mediator = "worth_listening",
                           robustSE = T, sims = 1000)
summary(med.worth.vote.b)
plot(med.follow.vote.b)


stargazer(worth_mediator, worth_mediator2, will_vote2_out_worth, vote_for_biden_ou_worth, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table2.html")





####

#  change in PBS t1 to t2
a1r_analysis_total$ideology_diff_13 =a1r_analysis_total$ideology_score_t1 - a1r_analysis_total$ideology_score_t3
a1r_analysis_total$ideology_diff_12 =a1r_analysis_total$ideology_score_t1 - a1r_analysis_total$ideology_score_t2

# mediator: ideology change
ideo_mediator= lmer(ideology_diff_12 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white+ (1|state_id) , 
                    data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                is.na(a1r_analysis_total$will_vote) == F,])
summary(ideo_mediator)




will_vote2_out_ideo = glmer(will_vote~ ideology_diff_12+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                            data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                        is.na(a1r_analysis_total$will_vote) == F,], family = 'binomial')
summary(will_vote2_out_ideo)

med.ideo.vote = mediate(ideo_mediator, will_vote2_out_ideo, treat = "delegate", mediator = "ideology_diff_12",
                        robustSE = T, sims = 1000)
summary(med.ideo.vote)




ideo_mediator2= lmer(ideology_diff_12 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                     data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                 is.na(a1r_analysis_total$simple_vote_biden) == F,])
summary(ideo_mediator2)

vote_for_biden__out_ideo = glmer(simple_vote_biden ~ ideology_diff_12+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                                 data = a1r_analysis_total[a1r_analysis_total$time > 2,], family = 'binomial')
summary(vote_for_biden__out_ideo)

med.ideo.vote.b = mediate(ideo_mediator2, vote_for_biden__out_ideo, treat = "delegate", mediator = "ideology_diff_12",
                          robustSE = T, sims = 1000)
summary(med.ideo.vote.b)
plot(med.ideo.vote.b)




stargazer(ideo_mediator, ideo_mediator2, will_vote2_out_ideo, vote_for_biden__out_ideo, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table4.html")




##### Do Knowledge as a mediator for vote at all + vote for Biden

#### Knowledge as a mediator: 

knowledge_mediator= lmer(knowledge_index_v2 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                         data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                     is.na(a1r_analysis_total$will_vote) == F,])

will_vote2_out_knowledge = glmer(will_vote~ knowledge_index_v2+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white + (1|state_id) , 
                                 data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                             is.na(a1r_analysis_total$will_vote) == F,], family = 'binomial')

med.knowledge.vote = mediate(knowledge_mediator, will_vote2_out_knowledge, 
                             treat = "delegate", mediator =  "knowledge_index_v2",
                             robustSE = T, sims = 1000)
summary(med.knowledge.vote)



knowledge_mediator.2= lmer(knowledge_index_v2 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                           data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                       is.na(a1r_analysis_total$simple_vote_biden) == F,])


vote_for_biden__out_knowledge = glmer(simple_vote_biden ~ knowledge_index_v2+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white + (1|state_id) , 
                                      data = a1r_analysis_total[a1r_analysis_total$time > 2,], family = 'binomial')

med.knowledge.vote.b = mediate(knowledge_mediator.2, vote_for_biden__out_knowledge, 
                               treat = "delegate", mediator = "knowledge_index_v2",
                               robustSE = T, sims = 1000)
summary(med.knowledge.vote.b)




stargazer(knowledge_mediator, knowledge_mediator.2, will_vote2_out_knowledge, vote_for_biden__out_knowledge, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table3.html")


####  Only Middle Group



a1r_analysis_total_middle = a1r_analysis_total[a1r_analysis_total$middle == 1,]


#####

# mediator_mid prediction: follow campaign


follow_mediator_mid= lmer(follow_campaign_all ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                          data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2,])
summary(follow_mediator_mid)



# out_midcome: vote at all 

will_vote2_out_mid = glmer(will_vote~ follow_campaign_all+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+  white+(1|state_id) , 
                           data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2,], family = 'binomial')
summary(will_vote2_out_mid)

med.follow.vote.mod = mediate(follow_mediator_mid, will_vote2_out_mid, treat = "delegate", mediator = "follow_campaign_all",
                              robustSE = T, sims = 1000)
summary(med.follow.vote.mod)
plot(med.follow.vote.mod)

# out_midcome: vote for biden 
# null finding
follow_mediator_mid_2= lmer(follow_campaign_all ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+  white+(1|state_id) , 
                            data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2 & 
                                                               is.na(a1r_analysis_total_middle$simple_vote_biden) == F,])
summary(follow_mediator_mid_2)
vote_for_biden_out_mid = glmer(simple_vote_biden ~ follow_campaign_all+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+  white+(1|state_id) , 
                               data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2,], family = 'binomial')
summary(vote_for_biden_out_mid)

med.follow.vote.b.mid = mediate(follow_mediator_mid_2, vote_for_biden_out_mid, treat = "delegate", mediator = "follow_campaign_all",
                                robustSE = T, sims = 1000)
summary(med.follow.vote.b.mid)
plot(med.follow.vote.b.mid)



stargazer(follow_mediator_mid, follow_mediator_mid_2, will_vote2_out_mid, vote_for_biden_out_mid, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table5.html")






# mediator_mid prediction: worth_listening


worth_mediator_mid= lmer(worth_listening ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                         data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                            is.na(a1r_analysis_total_middle$will_vote) == F,])
summary(worth_mediator_mid)



will_vote2_out_mid_worth = glmer(will_vote~ worth_listening+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                                 data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                                    is.na(a1r_analysis_total_middle$will_vote) == F,], family = 'binomial')
summary(will_vote2_out_mid_worth)

med.worth.vote.mid = mediate(worth_mediator_mid, will_vote2_out_mid_worth, treat = "delegate", mediator = "worth_listening",
                             robustSE = T, sims = 1000)
summary(med.worth.vote.mid)
plot(med.worth.vote.mid)

worth_mediator_mid2= lmer(worth_listening ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                          data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                             is.na(a1r_analysis_total_middle$simple_vote_biden) == F,])
summary(worth_mediator_mid2)

vote_for_biden_ou_worth.mid = glmer(simple_vote_biden ~ worth_listening+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                                    data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2,], family = 'binomial')
summary(vote_for_biden_ou_worth.mid)

med.worth.vote.b.mid = mediate(worth_mediator_mid2, vote_for_biden_ou_worth.mid, treat = "delegate", mediator = "worth_listening",
                               robustSE = T, sims = 1000)
summary(med.worth.vote.b.mid)
plot(med.worth.vote.b.mid)


stargazer(worth_mediator_mid, worth_mediator_mid2, will_vote2_out_mid_worth, vote_for_biden_ou_worth.mid, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table6.html")







# mediator_mid: ideology change
ideo_mediator_mid= lmer(ideology_diff_12 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +  (1|state_id) , 
                        data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                           is.na(a1r_analysis_total_middle$will_vote) == F,])
summary(ideo_mediator_mid)


will_vote2_out_mid_ideo = glmer(will_vote~ ideology_diff_12+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+  white +(1|state_id) , 
                                data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                                   is.na(a1r_analysis_total_middle$will_vote) == F,], family = 'binomial')
summary(will_vote2_out_mid_ideo)

med.ideo.vote.mid = mediate(ideo_mediator_mid, will_vote2_out_mid_ideo, treat = "delegate", mediator = "ideology_diff_12",
                            robustSE = T, sims = 1000)
summary(med.ideo.vote.mid)




ideo_mediator_mid2= lmer(ideology_diff_12 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+  white +(1|state_id) , 
                         data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                            is.na(a1r_analysis_total_middle$simple_vote_biden) == F,])
summary(ideo_mediator_mid2)

vote_for_biden_out_mid_ideo = lmer(simple_vote_biden ~ ideology_diff_12+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+  white +(1|state_id) , 
                                   data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2,])
summary(vote_for_biden_out_mid_ideo)

med.ideo.vote.b.mid = mediate(ideo_mediator_mid2, vote_for_biden_out_mid_ideo, treat = "delegate", mediator = "ideology_diff_12",
                              robustSE = T, sims = 1000)
summary(med.ideo.vote.b.mid)
plot(med.ideo.vote.b.mid)




stargazer(ideo_mediator_mid, ideo_mediator_mid2, will_vote2_out_mid_ideo, vote_for_biden_out_mid_ideo, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table8.html")




#### Knowledge as a mediator: middle only 

knowledge_mediator_middle= lmer(knowledge_index_v2 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                                   is.na(a1r_analysis_total_middle$will_vote) == F,])

will_vote2_out_middle_knowledge = glmer(will_vote~ knowledge_index_v2+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                                        data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                                           is.na(a1r_analysis_total_middle$will_vote) == F,], family = 'binomial')

med.knowledge.vote.middle = mediate(knowledge_mediator_middle, will_vote2_out_middle_knowledge, 
                                    treat = "delegate", mediator =  "knowledge_index_v2",
                                    robustSE = T, sims = 1000)
summary(med.knowledge.vote.middle)



knowledge_mediator_middle.2= lmer(knowledge_index_v2 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white + (1|state_id) , 
                                  data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                                     is.na(a1r_analysis_total_middle$simple_vote_biden) == F,])


vote_for_biden__out_middle_knowledge = glmer(simple_vote_biden ~ knowledge_index_v2+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white + (1|state_id) , 
                                             data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2,], family = 'binomial')

med.knowledge.vote.b.middle = mediate(knowledge_mediator_middle.2, vote_for_biden__out_middle_knowledge, 
                                      treat = "delegate", mediator = "knowledge_index_v2",
                                      robustSE = T, sims = 1000)
summary(med.knowledge.vote.b.middle)



stargazer(knowledge_mediator_middle, knowledge_mediator_middle.2, will_vote2_out_middle_knowledge, vote_for_biden__out_middle_knowledge, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table7.html")








###################################### time three regression predictions Table 8
library(modelsummary)
biden_ideo1 =glmer(simple_vote_biden ~ delegate*ideology_score_t1 +college_grad +male + over60 + lowinc+ white  + Democrat+(1|state_id) , 
                   data = a1r_analysis_total[a1r_analysis_total$time > 2,], family = 'binomial')

biden_ideo2 =glmer(simple_vote_biden ~ delegate*ideology_score_t2 +college_grad +male + over60 + lowinc+ white +(1|state_id)+Democrat , 
                   data = a1r_analysis_total[a1r_analysis_total$time > 2,], family = 'binomial')
biden_ideo3 =glmer(simple_vote_biden ~ delegate*ideology_score_t3 +college_grad +male + over60 + lowinc+ white +(1|state_id)+Democrat , 
                   data = a1r_analysis_total[a1r_analysis_total$time > 2,], family = 'binomial')

summary(biden_ideo1)
summary(biden_ideo2)
summary(biden_ideo3)

models = list(biden_ideo1, biden_ideo2, biden_ideo3)

modelsummary(models, 
             coef_omit = "coll|male|over|lowin|party|tercept", 
             gof_omit = "RMSE|ICC|Marg.", , stars = T
)
modelsummary(models, 
             #coef_omit = "coll|male|over|lowin|party|tercept", 
             gof_omit = "RMSE|ICC|Marg.", 
             output = "C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/biden_vote_appx.html", stars = T
)


##################
### New DVs
##################



# mediator prediction: follow campaign


follow_mediator_alt= lmer(follow_campaign_all ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                          data = a1r_analysis_total[a1r_analysis_total$time > 2 &
                                                      is.na(a1r_analysis_total$did_vote) ==F ,])
summary(follow_mediator_alt)


did_vote2_out = glmer(did_vote~ follow_campaign_all+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white+ (1|state_id) , 
                      data = a1r_analysis_total[a1r_analysis_total$time > 2,], family = 'binomial')
summary(did_vote2_out)

med.follow.d.vote = mediate(follow_mediator_alt, did_vote2_out, treat = "delegate", mediator = "follow_campaign_all",
                            robustSE = T, sims = 1000)
summary(med.follow.d.vote)

# outcome: vote for biden 

follow_mediator_2_alt= lmer(follow_campaign_all ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                            data = a1r_analysis_total[a1r_analysis_total$time > 2 & 
                                                        is.na(a1r_analysis_total$did_vote_biden) == F,])
summary(follow_mediator_2)
did_vote_biden_out = glmer(did_vote_biden ~ follow_campaign_all+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                           data = a1r_analysis_total[a1r_analysis_total$time > 2,], family = 'binomial')
summary(did_vote_biden_out)

med.follow.vote.b.alt = mediate(follow_mediator_2_alt, did_vote_biden_out, treat = "delegate", mediator = "follow_campaign_all",
                                robustSE = T, sims = 1000)
summary(med.follow.vote.b.alt)




stargazer(follow_mediator_alt, follow_mediator_2_alt, did_vote2_out, did_vote_biden_out, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.1.html")





worth_mediator_alt= lmer(worth_listening ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                         data = a1r_analysis_total[a1r_analysis_total$time > 2 &
                                                     is.na(a1r_analysis_total$did_vote) ==F ,])
summary(worth_mediator_alt)


did_vote2_out_worth = glmer(did_vote~ worth_listening+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                            data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                        is.na(a1r_analysis_total$did_vote) == F,], family = 'binomial')
summary(did_vote2_out_worth)

med.worth.vote.alt = mediate(worth_mediator_alt, did_vote2_out_worth, treat = "delegate", mediator = "worth_listening",
                             robustSE = T, sims = 1000)
summary(med.worth.vote.alt)




worth_mediator2_alt= lmer(worth_listening ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                          data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                      is.na(a1r_analysis_total$did_vote_biden) == F,])
summary(worth_mediator2_alt)

did_vote_biden_ou_worth = glmer(did_vote_biden ~ worth_listening+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                data = a1r_analysis_total[a1r_analysis_total$time > 2,], family = 'binomial')
summary(did_vote_biden_ou_worth)

med.worth.vote.b.alt = mediate(worth_mediator2_alt, did_vote_biden_ou_worth, treat = "delegate", mediator = "worth_listening",
                               robustSE = T, sims = 1000)
summary(med.worth.vote.b.alt)


stargazer(worth_mediator_alt, worth_mediator2_alt, did_vote2_out_worth, did_vote_biden_ou_worth, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.2.html")




#Knowledge as mediator

knowledge_mediator_alt= lmer(knowledge_index_v2 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                             data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                         is.na(a1r_analysis_total$did_vote) == F,])

did_vote2_out_knowledge = glmer(did_vote~ knowledge_index_v2+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                            is.na(a1r_analysis_total$did_vote) == F,], family = 'binomial')

med.knowledge.vote.alt = mediate(knowledge_mediator_alt, did_vote2_out_knowledge, 
                                 treat = "delegate", mediator =  "knowledge_index_v2",
                                 robustSE = T, sims = 1000)
summary(med.knowledge.vote.alt)



knowledge_mediator.2.alt = lmer(knowledge_index_v2 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                            is.na(a1r_analysis_total$did_vote_biden) == F,])


did_vote_biden__out_knowledge = glmer(did_vote_biden ~ knowledge_index_v2+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white +  (1|state_id) , 
                                      data = a1r_analysis_total[a1r_analysis_total$time > 2,], family = 'binomial')

med.knowledge.vote.b.alt = mediate(knowledge_mediator.2.alt, did_vote_biden__out_knowledge, 
                                   treat = "delegate", mediator = "knowledge_index_v2",
                                   robustSE = T, sims = 1000)
summary(med.knowledge.vote.b.alt)

stargazer(knowledge_mediator_alt, knowledge_mediator.2.alt, did_vote2_out_knowledge, did_vote_biden__out_knowledge, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.3.html")




# PBS change

ideo_mediator_alt= lmer(ideology_diff_12 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                        data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                    is.na(a1r_analysis_total$did_vote) == F,])
summary(ideo_mediator_alt)

did_vote2_out_ideo = glmer(did_vote~ ideology_diff_12+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                           data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                       is.na(a1r_analysis_total$did_vote) == F,], family = 'binomial')
summary(did_vote2_out_ideo)

med.ideo.vote.alt = mediate(ideo_mediator_alt, did_vote2_out_ideo, treat = "delegate", mediator = "ideology_diff_12",
                            robustSE = T, sims = 1000)
summary(med.ideo.vote.alt)




ideo_mediator2_alt= lmer(ideology_diff_12 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white +  (1|state_id) , 
                         data = a1r_analysis_total[a1r_analysis_total$time > 2&
                                                     is.na(a1r_analysis_total$did_vote_biden) == F,])
summary(ideo_mediator2_alt)

did_vote_biden__out_ideo = glmer(did_vote_biden ~ ideology_diff_12+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                 data = a1r_analysis_total[a1r_analysis_total$time > 2,], family = 'binomial')
summary(vote_for_biden__out_ideo)

med.ideo.vote.b.alt = mediate(ideo_mediator2_alt, did_vote_biden__out_ideo, treat = "delegate", mediator = "ideology_diff_12",
                              robustSE = T, sims = 1000)
summary(med.ideo.vote.b.alt)


stargazer(ideo_mediator_alt, ideo_mediator2_alt, did_vote2_out_ideo, vote_for_biden__out_ideo, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.4.html")




##### middle only

a1r_analysis_total_middle = a1r_analysis_total_middle[!duplicated(as.list(a1r_analysis_total_middle))]

a1r_analysis_total_middle = a1r_analysis_total_middle %>%
  left_join(t5)


a1r_analysis_total_middle$did_vote = 0
a1r_analysis_total_middle$did_vote[a1r_analysis_total_middle$VOTE20 == "I'm sure I voted."] = 1
a1r_analysis_total_middle$did_vote[is.na(a1r_analysis_total_middle$VOTE20) == T] = NA


a1r_analysis_total_middle$did_vote_biden = 0
a1r_analysis_total_middle$did_vote_biden[a1r_analysis_total_middle$CANDI20 == "Biden/Harris"] = 1
a1r_analysis_total_middle$did_vote_biden[is.na(a1r_analysis_total_middle$CANDI20) == T] = NA




# mediator prediction: follow campaign


follow_mediator_alt_middle= lmer(follow_campaign_all ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                 data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2 &
                                                                    is.na(a1r_analysis_total_middle$did_vote) ==F ,])
summary(follow_mediator_alt_middle)


did_vote2_out_middle = glmer(did_vote~ follow_campaign_all+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                             data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2,], family = 'binomial')
summary(did_vote2_out_middle)

med.follow.d.vote.middle = mediate(follow_mediator_alt_middle, did_vote2_out_middle, treat = "delegate", mediator = "follow_campaign_all",
                                   robustSE = T, sims = 1000)
summary(med.follow.d.vote.middle)

# outcome: vote for biden 

follow_mediator_2_alt_middle= lmer(follow_campaign_all ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                   data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2 & 
                                                                      is.na(a1r_analysis_total_middle$did_vote_biden) == F,])
summary(follow_mediator_2)
did_vote_biden_out_middle = glmer(did_vote_biden ~ follow_campaign_all+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                  data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2,], family = 'binomial')
summary(did_vote_biden_out_middle)

med.follow.vote.b.alt_middle = mediate(follow_mediator_2_alt_middle, did_vote_biden_out_middle, treat = "delegate", mediator = "follow_campaign_all",
                                       robustSE = T, sims = 1000)
summary(med.follow.vote.b.alt_middle)





stargazer(follow_mediator_alt_middle, follow_mediator_2_alt_middle, did_vote2_out_middle, did_vote_biden_out_middle, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.5.html")






worth_mediator_alt_middle= lmer(worth_listening ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2 &
                                                                   is.na(a1r_analysis_total_middle$did_vote) ==F ,])
summary(worth_mediator_alt_middle)


did_vote2_out_worth_middle = glmer(did_vote~ worth_listening+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                   data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                                      is.na(a1r_analysis_total_middle$did_vote) == F,], family = 'binomial')
summary(did_vote2_out_worth_middle)

med.worth.vote.alt_middle = mediate(worth_mediator_alt_middle, did_vote2_out_worth_middle, treat = "delegate", mediator = "worth_listening",
                                    robustSE = T, sims = 1000)
summary(med.worth.vote.alt_middle)



worth_mediator2_alt_middle= lmer(worth_listening ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                 data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                                    is.na(a1r_analysis_total_middle$did_vote_biden) == F,])
summary(worth_mediator2_alt_middle)

did_vote_biden_ou_worth_middle = glmer(did_vote_biden ~ worth_listening+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                       data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2,], family = 'binomial')
summary(did_vote_biden_ou_worth_middle)

med.worth.vote.b.alt_middle = mediate(worth_mediator2_alt_middle, did_vote_biden_ou_worth_middle, treat = "delegate", mediator = "worth_listening",
                                      robustSE = T, sims = 1000)
summary(med.worth.vote.b.alt_middle)



stargazer(worth_mediator_alt_middle, worth_mediator2_alt_middle, did_vote2_out_worth_middle, did_vote_biden_ou_worth_middle, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.6.html")




#Knowledge as mediator

knowledge_mediator_alt_middle= lmer(knowledge_index_v2 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white +  (1|state_id) , 
                                    data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                                       is.na(a1r_analysis_total_middle$did_vote) == F,])

did_vote2_out_knowledge_middle = glmer(did_vote~ knowledge_index_v2+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                       data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                                          is.na(a1r_analysis_total_middle$did_vote) == F,], family = 'binomial')

med.knowledge.vote.alt_middle = mediate(knowledge_mediator_alt_middle, did_vote2_out_knowledge_middle, 
                                        treat = "delegate", mediator =  "knowledge_index_v2",
                                        robustSE = T, sims = 1000)
summary(med.knowledge.vote.alt_middle)



knowledge_mediator.2.alt_middle = lmer(knowledge_index_v2 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                       data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                                          is.na(a1r_analysis_total_middle$did_vote_biden) == F,])


did_vote_biden__out_knowledge_middle = glmer(did_vote_biden ~ knowledge_index_v2+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white +  (1|state_id) , 
                                             data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2,], family = 'binomial')

med.knowledge.vote.b.alt_middle = mediate(knowledge_mediator.2.alt_middle, did_vote_biden__out_knowledge_middle, 
                                          treat = "delegate", mediator = "knowledge_index_v2",
                                          robustSE = T, sims = 1000)
summary(med.knowledge.vote.b.alt_middle)


stargazer(knowledge_mediator_alt_middle, knowledge_mediator.2.alt_middle, did_vote2_out_knowledge_middle, did_vote_biden__out_knowledge_middle, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.7.html")



# PBS change

ideo_mediator_alt_middle= lmer(ideology_diff_12 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white +  (1|state_id) , 
                               data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                                  is.na(a1r_analysis_total_middle$did_vote) == F,])
summary(ideo_mediator_alt_middle)

did_vote2_out_ideo_middle = glmer(did_vote~ ideology_diff_12+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                  data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                                     is.na(a1r_analysis_total_middle$did_vote) == F,], family = 'binomial')
summary(did_vote2_out_ideo_middle)

med.ideo.vote.alt_middle = mediate(ideo_mediator_alt_middle, did_vote2_out_ideo_middle, treat = "delegate", mediator = "ideology_diff_12",
                                   robustSE = T, sims = 1000)
summary(med.ideo.vote.alt_middle)




ideo_mediator2_alt_middle= lmer(ideology_diff_12 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white +  (1|state_id) , 
                                data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2&
                                                                   is.na(a1r_analysis_total_middle$did_vote_biden) == F,])
summary(ideo_mediator2_alt_middle)

did_vote_biden__out_ideo_middle = glmer(did_vote_biden ~ ideology_diff_12+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white +  (1|state_id) , 
                                        data = a1r_analysis_total_middle[a1r_analysis_total_middle$time > 2,], family = 'binomial')
summary(did_vote_biden__out_ideo_middle)

med.ideo.vote.b.alt_middle = mediate(ideo_mediator2_alt_middle, did_vote_biden__out_ideo_middle, treat = "delegate", mediator = "ideology_diff_12",
                                     robustSE = T, sims = 1000)
summary(med.ideo.vote.b.alt_middle)


stargazer(ideo_mediator_alt_middle, ideo_mediator2_alt_middle, did_vote2_out_ideo_middle, did_vote_biden__out_ideo_middle, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.8.html")




#confirmed did vote


follow_mediator_alt_a= lmer(follow_campaign_all ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                            data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2 &
                                                                  is.na(a1r_analysis_total_confirmed$did_vote_val) ==F ,])
summary(follow_mediator_alt_a)


did_vote_val2_out = glmer(did_vote_val~ follow_campaign_all+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                          data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2,], family = 'binomial')
summary(did_vote_val2_out)

med.follow.d.vote = mediate(follow_mediator_alt_a, did_vote_val2_out, treat = "delegate", mediator = "follow_campaign_all",
                            robustSE = T, sims = 1000)
summary(med.follow.d.vote)

# outcome: vote for biden 

follow_mediator_2_alt_a= lmer(follow_campaign_all ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                              data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2 & 
                                                                    is.na(a1r_analysis_total_confirmed$did_vote_val_biden) == F,])
summary(follow_mediator_2)
did_vote_val_biden_out = glmer(did_vote_val_biden ~ follow_campaign_all+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                               data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2,], family = 'binomial')
summary(did_vote_val_biden_out)

med.follow.vote.b.alt_a = mediate(follow_mediator_2_alt_a, did_vote_val_biden_out, treat = "delegate", mediator = "follow_campaign_all",
                                  robustSE = T, sims = 1000)
summary(med.follow.vote.b.alt_a)




stargazer(follow_mediator_alt_a, follow_mediator_2_alt_a, did_vote_val2_out, did_vote_val_biden_out, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.app.1.html")






worth_mediator_alt_a= lmer(worth_listening ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                           data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2 &
                                                                 is.na(a1r_analysis_total_confirmed$did_vote_val) ==F ,])
summary(worth_mediator_alt_a)


did_vote_val2_out_worth = glmer(did_vote_val~ worth_listening+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                                data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2&
                                                                      is.na(a1r_analysis_total_confirmed$did_vote_val) == F,], family = 'binomial')
summary(did_vote_val2_out_worth)

med.worth.vote.alt_a = mediate(worth_mediator_alt_a, did_vote_val2_out_worth, treat = "delegate", mediator = "worth_listening",
                               robustSE = T, sims = 1000)
summary(med.worth.vote.alt_a)




worth_mediator2_alt_a= lmer(worth_listening ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white+ (1|state_id) , 
                            data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2&
                                                                  is.na(a1r_analysis_total_confirmed$did_vote_val_biden) == F,])
summary(worth_mediator2_alt_a)

did_vote_val_biden_ou_worth = glmer(did_vote_val_biden ~ worth_listening+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white+(1|state_id) , 
                                    data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2,], family = 'binomial')
summary(did_vote_val_biden_ou_worth)

med.worth.vote.b.alt_a = mediate(worth_mediator2_alt_a, did_vote_val_biden_ou_worth, treat = "delegate", mediator = "worth_listening",
                                 robustSE = T, sims = 1000)
summary(med.worth.vote.b.alt_a)



stargazer(worth_mediator_alt_a, worth_mediator2_alt_a, did_vote_val2_out_worth, did_vote_val_biden_ou_worth, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.app.2.html")





#Knowledge as mediator

knowledge_mediator_alt_a= lmer(knowledge_index_v2 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                               data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2&
                                                                     is.na(a1r_analysis_total_confirmed$did_vote_val) == F,])

did_vote_val2_out_knowledge = glmer(did_vote_val~ knowledge_index_v2+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                                    data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2&
                                                                          is.na(a1r_analysis_total_confirmed$did_vote_val) == F,], family = 'binomial')

med.knowledge.vote.alt_a = mediate(knowledge_mediator_alt_a, did_vote_val2_out_knowledge, 
                                   treat = "delegate", mediator =  "knowledge_index_v2",
                                   robustSE = T, sims = 1000)
summary(med.knowledge.vote.alt_a)



knowledge_mediator.2.alt_a = lmer(knowledge_index_v2 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                                  data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2&
                                                                        is.na(a1r_analysis_total_confirmed$did_vote_val_biden) == F,])


did_vote_val_biden__out_knowledge = glmer(did_vote_val_biden ~ knowledge_index_v2+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                                          data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2,], family = 'binomial')

med.knowledge.vote.b.alt_a = mediate(knowledge_mediator.2.alt_a, did_vote_val_biden__out_knowledge, 
                                     treat = "delegate", mediator = "knowledge_index_v2",
                                     robustSE = T, sims = 1000)
summary(med.knowledge.vote.b.alt_a)




stargazer(knowledge_mediator_alt_a, knowledge_mediator.2.alt_a, did_vote_val2_out_knowledge, did_vote_val_biden__out_knowledge, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.app.3.html")




# PBS change

ideo_mediator_alt_a= lmer(ideology_diff_12 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                          data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2&
                                                                is.na(a1r_analysis_total_confirmed$did_vote_val) == F,])
summary(ideo_mediator_alt_a)

did_vote_val2_out_ideo = glmer(did_vote_val~ ideology_diff_12+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white + (1|state_id) , 
                               data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2&
                                                                     is.na(a1r_analysis_total_confirmed$did_vote_val) == F,], family = 'binomial')
summary(did_vote_val2_out_ideo)

med.ideo.vote.alt_a = mediate(ideo_mediator_alt_a, did_vote_val2_out_ideo, treat = "delegate", mediator = "ideology_diff_12",
                              robustSE = T, sims = 1000)
summary(med.ideo.vote.alt_a)




ideo_mediator2_alt_a= lmer(ideology_diff_12 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                           data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2&
                                                                 is.na(a1r_analysis_total_confirmed$did_vote_val_biden) == F,])
summary(ideo_mediator2_alt_a)

did_vote_val_biden__out_ideo = glmer(did_vote_val_biden ~ ideology_diff_12+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                                     data = a1r_analysis_total_confirmed[a1r_analysis_total_confirmed$time > 2,], family = 'binomial')
summary(vote_for_biden__out_ideo)

med.ideo.vote.b.alt_a = mediate(ideo_mediator2_alt_a, did_vote_val_biden__out_ideo, treat = "delegate", mediator = "ideology_diff_12",
                                robustSE = T, sims = 1000)
summary(med.ideo.vote.b.alt_a)





stargazer(ideo_mediator_alt_a, ideo_mediator2_alt_a, did_vote_val2_out_ideo, did_vote_val_biden__out_ideo, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.app.4.html")




##################### Middle 



a1r_analysis_total_confirmed_middle 







follow_mediator_alt_middle= lmer(follow_campaign_all ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                 data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2 &
                                                                              is.na(a1r_analysis_total_confirmed_middle$did_vote_val) ==F ,])
summary(follow_mediator_alt_middle)


did_vote_val2_out_middle = glmer(did_vote_val~ follow_campaign_all+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                 data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2,], family = 'binomial')
summary(did_vote_val2_out_middle)

med.follow.d.vote.middle = mediate(follow_mediator_alt_middle, did_vote_val2_out_middle, treat = "delegate", mediator = "follow_campaign_all",
                                   robustSE = T, sims = 1000)
summary(med.follow.d.vote.middle)

# outcome: vote for biden 

follow_mediator_2_alt_middle= lmer(follow_campaign_all ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white +  (1|state_id) , 
                                   data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2 & 
                                                                                is.na(a1r_analysis_total_confirmed_middle$did_vote_val_biden) == F,])
summary(follow_mediator_2)
did_vote_val_biden_out_middle = glmer(did_vote_val_biden ~ follow_campaign_all+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white +  (1|state_id) , 
                                      data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2,], family = 'binomial')
summary(did_vote_val_biden_out_middle)

med.follow.vote.b.alt_middle = mediate(follow_mediator_2_alt_middle, did_vote_val_biden_out_middle, treat = "delegate", mediator = "follow_campaign_all",
                                       robustSE = T, sims = 1000)
summary(med.follow.vote.b.alt_middle)



stargazer(follow_mediator_alt_middle, follow_mediator_2_alt_middle, did_vote_val2_out_middle, did_vote_val_biden_out_middle, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.app.5.html")







worth_mediator_alt_middle= lmer(worth_listening ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                                data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2 &
                                                                             is.na(a1r_analysis_total_confirmed_middle$did_vote_val) ==F ,])
summary(worth_mediator_alt_middle)


did_vote_val2_out_worth_middle = glmer(did_vote_val~ worth_listening+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                                       data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2&
                                                                                    is.na(a1r_analysis_total_confirmed_middle$did_vote_val) == F,], family = 'binomial')
summary(did_vote_val2_out_worth_middle)

med.worth.vote.alt_middle = mediate(worth_mediator_alt_middle, did_vote_val2_out_worth_middle, treat = "delegate", mediator = "worth_listening",
                                    robustSE = T, sims = 1000)
summary(med.worth.vote.alt_middle)




worth_mediator2_alt_middle= lmer(worth_listening ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                                 data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2&
                                                                              is.na(a1r_analysis_total_confirmed_middle$did_vote_val_biden) == F,])
summary(worth_mediator2_alt_middle)

did_vote_val_biden_ou_worth_middle = glmer(did_vote_val_biden ~ worth_listening+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white +(1|state_id) , 
                                           data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2,], family = 'binomial')
summary(did_vote_val_biden_ou_worth_middle)

med.worth.vote.b.alt_middle = mediate(worth_mediator2_alt_middle, did_vote_val_biden_ou_worth_middle, treat = "delegate", mediator = "worth_listening",
                                      robustSE = T, sims = 1000)
summary(med.worth.vote.b.alt_middle)


stargazer(worth_mediator_alt_middle, worth_mediator2_alt_middle, did_vote_val2_out_worth_middle, did_vote_val_biden_ou_worth_middle, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.app.6.html")





#Knowledge as mediator

knowledge_mediator_alt_middle= lmer(knowledge_index_v2 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                    data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2&
                                                                                 is.na(a1r_analysis_total_confirmed_middle$did_vote_val) == F,])

did_vote_val2_out_knowledge_middle = glmer(did_vote_val~ knowledge_index_v2+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white +  (1|state_id) , 
                                           data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2&
                                                                                        is.na(a1r_analysis_total_confirmed_middle$did_vote_val) == F,], family = 'binomial')

med.knowledge.vote.alt_middle = mediate(knowledge_mediator_alt_middle, did_vote_val2_out_knowledge_middle, 
                                        treat = "delegate", mediator =  "knowledge_index_v2",
                                        robustSE = T, sims = 1000)
summary(med.knowledge.vote.alt_middle)



knowledge_mediator.2.alt_middle = lmer(knowledge_index_v2 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                       data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2&
                                                                                    is.na(a1r_analysis_total_confirmed_middle$did_vote_val_biden) == F,])


did_vote_val_biden__out_knowledge_middle = glmer(did_vote_val_biden ~ knowledge_index_v2+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white +  (1|state_id) , 
                                                 data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2,], family = 'binomial')

med.knowledge.vote.b.alt_middle = mediate(knowledge_mediator.2.alt_middle, did_vote_val_biden__out_knowledge_middle, 
                                          treat = "delegate", mediator = "knowledge_index_v2",
                                          robustSE = T, sims = 1000)
summary(med.knowledge.vote.b.alt_middle)


stargazer(knowledge_mediator_alt_middle, knowledge_mediator.2.alt_middle, did_vote_val2_out_knowledge_middle, did_vote_val_biden__out_knowledge_middle, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.app.7.html")


# PBS change

ideo_mediator_alt_middle= lmer(ideology_diff_12 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                               data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2&
                                                                            is.na(a1r_analysis_total_confirmed_middle$did_vote_val) == F,])
summary(ideo_mediator_alt_middle)

did_vote_val2_out_ideo_middle = glmer(did_vote_val~ ideology_diff_12+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                      data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2&
                                                                                   is.na(a1r_analysis_total_confirmed_middle$did_vote_val) == F,], family = 'binomial')
summary(did_vote_val2_out_ideo_middle)

med.ideo.vote.alt_middle = mediate(ideo_mediator_alt_middle, did_vote_val2_out_ideo_middle, treat = "delegate", mediator = "ideology_diff_12",
                                   robustSE = T, sims = 1000)
summary(med.ideo.vote.alt_middle)




ideo_mediator2_alt_middle= lmer(ideology_diff_12 ~  populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+ white + (1|state_id) , 
                                data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2&
                                                                             is.na(a1r_analysis_total_confirmed_middle$did_vote_val_biden) == F,])
summary(ideo_mediator2_alt_middle)

did_vote_val_biden__out_ideo_middle = glmer(did_vote_val_biden ~ ideology_diff_12+ populism_score + delegate + ideology_score_t1 +college_grad +male + over60 + lowinc+white +  (1|state_id) , 
                                            data = a1r_analysis_total_confirmed_middle[a1r_analysis_total_confirmed_middle$time > 2,], family = 'binomial')
summary(did_vote_val_biden__out_ideo_middle)

med.ideo.vote.b.alt_middle = mediate(ideo_mediator2_alt_middle, did_vote_val_biden__out_ideo_middle, treat = "delegate", mediator = "ideology_diff_12",
                                     robustSE = T, sims = 1000)
summary(med.ideo.vote.b.alt_middle)


stargazer(ideo_mediator_alt_middle, ideo_mediator2_alt_middle, did_vote_val2_out_ideo_middle, did_vote_val_biden__out_ideo_middle, no.space = T,
          out="C:/Users/lerner-josh/Dropbox/America1Room/reg_data/r/output/appendix.table.alt.app.8.html")



